//获取应用实例
var app = getApp();

var FormCheckRule = {
	nameRule: /^[\u4e00-\u9fa5]{1,12}$/,
	passErrorMessage:"请输入正确的通行证号！",
	surnameNullMessage:"姓氏不能为空！",
	surnameErrorMessage: "请输入正确的姓氏",
	nameNullMessage:"名字不能为空！",
	nameErrorMessage:"请输入正确的名字",
	sexNullMessage: "性别不能为空",
	birthdayNullMessage: "出生日期不能为空",
	validateEndNullMessage: "通行证有效期不能为空",
	issueProvinceIdNullMessage: "签发地点不能为空"
};

Page({
    data: {
     "isShowFootNav": false,
      "GenderData": [{index: 0,check:1,name:'男'},{index:1,check:2,name:'女'}],
      "genderName": "请选择性别",
	    "genderType": "",
      "currentData": "",  //当前时间，时间组件
      "birthday": "", //选择的出生日期birthday
      "validateEnd": "", //有效时间
	    "index": "", //签发地下标
	    "id": "", //通行证ID
	    "disabled": false, //是否能够输入
	    "btnIsShow": false,  //是否显示按钮
	    "objectArray": [
	    	{id: 0, name: '北京市',label: '110000'}, {id: 1, name: '天津市',label: '120000'}, {id: 2, name: '河北省',label: '130000'},{id: 3, name: '山西省',label: '140000'},{id: 4, name: '内蒙古',label: '150000'},{id: 5, name: '辽宁省',label: '210000'},
	    	{id: 6, name: '吉林省',label: '220000'}, {id: 7, name: '黑龙江',label: '230000'}, {id: 8, name: '上海市',label: '310000'},{id:9, name: '江苏省',label: '320000'},{id: 10, name: '浙江省',label: '330000'},{id: 11, name: '安徽省',label: '340000'},
	    	{id: 12, name: '福建省',label: '350000'}, {id: 13, name: '江西省',label: '360000'}, {id: 14, name: '山东省',label: '370000'},{id:15, name: '河南省',label: '410000'},{id: 16, name: '湖北省',label: '420000'},{id: 17, name: '湖南省',label: '430000'},
	    	{id: 18, name: '广东省',label: '440000'}, {id: 19, name: '广西',label: '450000'}, {id: 20, name: '海南省',label: '460000'},{id:21, name: '重庆市',label: '500000'},{id: 22, name: '四川省',label: '510000'},{id: 23, name: '贵州省',label: '520000'},
	    	{id: 24, name: '云南省',label: '530000'}, {id: 25, name: '西藏',label: '540000'}, {id: 26, name: '陕西省',label: '610000'},{id:27, name: '甘肃省',label: '620000'},{id: 28, name: '青海省',label: '630000'},{id: 29, name: '宁夏',label: '640000'},
		    {id: 30, name: '新疆',label: '650000'}
	    ]
    },
    onLoad: function (option) {
	    this.setData({
	    	id: option.id,
		    currentData: this.getBirthDate()
      })

	    switch (option.type){
		    case 'disabled':
				this.queryPass()
		    this.setData({
		    	disabled: true,
			    btnIsShow: true
		    })
			    wx.setNavigationBarTitle({
				    title: '编辑港澳通行证',
			    })
			    break;
		    default:
			    wx.setNavigationBarTitle({
				    title: '添加港澳通行证',
			    })
			    break
	    }
	    app.setStorageSync(app.globalData.callVisaSign,1)  //用于首页是否调用签注类别标记

    },

    onHide:function () {

  	},

		//查询港澳证件
		queryPass:function(){
    	var that = this;
			var postData={
				id: this.data.id
			}
			var opt1 = {
				url: app.globalData.host+"/wx/ehk/ghk.ajax",
				data: postData,
				sucFn:function (data) {
					var res = data.body, issueProvinceName='',index='';
					for(var i=0;i<that.data.objectArray.length;i++){
						if(that.data.objectArray[i].label == res.issueProvinceId){
							issueProvinceName = that.data.objectArray[i].name
							index = that.data.objectArray[i].id
						}
					}
					that.setData({
						pass: res.passCode,
						surname: res.passNameM,
						name: res.passNameX,
						genderName: res.sex==1?'男':res.sex==2?'女':'',
						genderType: res.sex,
						typeStyle: "color: #1a1a1a;font-size:34rpx",
						defaultBirthTextHide: true,
						birthday: app.toYMDDate(res.birthday),
						defaultValidityTextHide: true,
						validateEnd: app.toYMDDate(res.validateEnd),
						defaultIssueTextHide: true,
						issueProvinceName: issueProvinceName,
						index: index
					})
				}
			}
			app.ajax(opt1)
		},

		getBirthDate: function () {
			/*
			 *   出生日期组件获得结束时间
			 * */
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth()+1;
			var day = date.getDate();
			return year+'-'+month+'-'+day
		},

    //选择出生日期
	  bindBirthDateChange: function(e) {
      this.setData({
	      birthday: e.detail.value,
	      defaultBirthTextHide: true
      })
    },

    //有效期日期
    bindValidityDateChange: function (e) {
	    this.setData({
		    validateEnd: e.detail.value,
		    defaultValidityTextHide: true
	    })
    },

    //签发地
	  bindIssuePickerChange: function(e) {
      this.setData({
        index: e.detail.value,
	      defaultIssueTextHide: true
      })
    },

    //确认提交按钮
	  formSubmit: function(e) {
    	var formData = e.detail.value;
    	var pass = formData.pass;
    	var surname = formData.surname;
    	var name = formData.name;
			var that = this;
	    var flag = app.check([
		    {
			    value: pass,
			    errorFocusName:"pass",
			    fn: function(value){
				    if(value == ''){
					    app.errorToast(FormCheckRule.passErrorMessage);
					    return false;
				    }else {
					    return true;
				    }
			    }
		    }, {
			    value: surname,
			    errorFocusName:"surname",
			    fn: function(value){
				    if(value == ''){
					    app.errorToast(FormCheckRule.surnameNullMessage);
					    return false;
				    }else if(FormCheckRule.nameRule.test(value)){
					    return true;
				    }else {
					    app.errorToast(FormCheckRule.surnameErrorMessage);
					    return false;
				    }
			    }
		    },{
			    value: name,
			    errorFocusName:"name",
			    fn: function(value){
				    if(value == ''){
					    app.errorToast(FormCheckRule.nameNullMessage);
					    return false;
				    }else if(FormCheckRule.nameRule.test(value)){
					    return true;
				    }else {
					    app.errorToast(FormCheckRule.nameErrorMessage);
					    return false;
				    }
			    }
		    },{
	    	  value: this.data.genderName,
			    errorFocusName: "genderName",
			    fn: function (value) {
				    if(value=="请选择性别"){
					    app.errorToast(FormCheckRule.sexNullMessage);
					    return false;
				    }else {
				    	return true;
				    }
			    }
		    },{
			    value: this.data.birthday,
			    errorFocusName: "birthday",
			    fn: function (value) {
				    if(!value){
					    app.errorToast(FormCheckRule.birthdayNullMessage);
					    return false;
				    }else {
					    return true;
				    }
			    }
		    },{
			    value: this.data.validateEnd,
			    errorFocusName: "birthday",
			    fn: function (value) {
				    if(!value){
					    app.errorToast(FormCheckRule.validateEndNullMessage);
					    return false;
				    }else {
					    return true;
				    }
			    }
		    },{
			    value: this.data.index,
			    errorFocusName: "birthday",
			    fn: function (value) {
				    if(value===''){
					    app.errorToast(FormCheckRule.issueProvinceIdNullMessage);
					    return false;
				    }else {
					    return true;
				    }
			    }
		    }
	    ]);
	    if (!flag) {
		    return false;
	    }

			var postData = {
				birthday: this.data.birthday,
				id: this.data.id,
				IssueOffice: this.data.objectArray[this.data.index].label,
				IssueProvinceId: this.data.objectArray[this.data.index].label,
				IssueProvinceName: this.data.objectArray[this.data.index].name,
				passCode: pass,
				passName: surname+name,
				passNameM: surname,
				passNameX: name,
				sex: this.data.genderType,
				validateEnd: this.data.validateEnd
			};
		  var opt = {
			  url: app.globalData.host+"/wx/ehk/vali.ajax",
			  data: postData,
			  sucFn:function (result) {
				  if(result.code == 1000){
					  var resultState = result.body;
					  if(resultState.result == 0){
						  app.errorToast(resultState.failReson);
						  return;
					  }else {
						  var opt1 = {
							  url: app.globalData.host+"/wx/ehk/edit.ajax",
							  data: postData,
							  sucFn:function (data) {
							  	if(data.code == 1000){
									  if(!that.data.disabled){
										  app.setStorageSync('addPass',1)
									  }
									  wx.navigateBack();   //返回上一个页面
								  }
							  }
						  }
						  app.ajax(opt1)
					  }
				  }
			  }
		  };
		  app.ajax(opt);
    },

		//删除通行证接口
		cancel: function () {
			var that = this;
			wx.showModal({
				title: '',
				content: '删除通行证，您将看不到您证件信息，请慎重操作!',
				success: function(res) {
					if (res.confirm) {
						var opt = {
							url: app.globalData.host+"/wx/ehk/dl.ajax",
							data: {
								id: that.data.id
							},
							sucFn:function (result) {
								app.errorToast('删除成功');
								setTimeout(function () {
									wx.navigateBack();   //返回上一个页面
								},1000)
							}
						}
						app.ajax(opt)
					} else if (res.cancel) {
						console.log('用户点击取消')
					}
				}
			})

		},
    //显示性别类别
    showGenderSelection: function () {
      this.setData({
	      isCanScroll:true,
	      isShowGenderList: true
      })
    },
    //选择性别类别
	  genderSelection: function(e){
      this.setData({
        isCanScroll:true,
	      isShowGenderList: true,
        typeStyle: "color: #1a1a1a;font-size:34rpx",
	      genderName: e.target.dataset.name,
	      genderType: e.target.dataset.type
      });
    },

    //点击取消按钮取默认状态
    cancelBtnGenderSelection: function () {
      var that = this;
      setTimeout(function(){
        that.setData({
          isCanScroll:true,
	        isShowGenderList: false
        });
      },50)
    },

    //点击mask隐藏性别列表
    hideGenderSelection: function () {
      var that = this;
      setTimeout(function(){
        that.setData({
          isCanScroll:true,
	        isShowGenderList: false,
        });
      },50)
    },


    //输入框公用方法
    infoInput: function(e){
      var id = e.currentTarget.id,
        value = e.detail.value;
      if(id=="pass"){  //通行证号
	      var num = ['0','1','2','3','4','5','6','7','8','9'];
	      if(value.length==0){
		      this.setData({
			      pass: value,
			      passIconType: 'success'
		      })
	      } else if(value.length==1){
		      if(value.substr(0,1) == 'w'||value.substr(0,1) == 'W'||value.substr(0,1) == 'c'||value.substr(0,1) == 'C'){
			      this.setData({
				      pass: value.toUpperCase().replace(/(^\s*)|(\s*$)/g, ""),
				      passIconType: 'error'
			      })
		      }else {
		        this.setData({
		          pass: '',
			        passIconType: 'success'
            })
          }
	      }else{
		      for(var i=0;i<value.length;i++) {
			      if(num.indexOf(value[value.length - 1])>-1){
				      this.setData({
					      pass: value.replace(/(^\s*)|(\s*$)/g, "")
				      })
			      }else {
				      this.setData({
					      pass: value.replace(value[value.length - 1], "")
				      })
			      }
		      }
		      if(value.length<9){
			      this.setData({
				      passIconType: 'error'
			      })
		      }else {
			      this.setData({
				      passIconType: 'success'
			      })
		      }
	      }
      }else if(id == 'surname'){   //姓氏
	      var surname = value.replace(/(^\s*)|(\s*$)/g, "");
	      this.setData({
		      surname: surname
	      });
	      if (surname==''||FormCheckRule.nameRule.test(surname)) {
		      this.setData({surnameIconType: "success"});
	      } else if (value.length > 0) {
		      this.setData({surnameIconType: "error"});
	      }else {
		      this.setData({surnameIconType: ""});
	      }
      }else if(id == 'name'){  //名字
	      var name = value.replace(/(^\s*)|(\s*$)/g, "");
	      this.setData({
		      name: name
	      });
	      if (name==''||FormCheckRule.nameRule.test(name)) {
		      this.setData({nameIconType: "success"});
	      } else if (value.length > 0) {
		      this.setData({nameIconType: "error"});
	      }else {
		      this.setData({nameIconType: ""});
	      }
      }
    }
});


